package com.gaogzhen.security.lock.reentrant;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 测试ReentrantLock可重入性
 */
@Slf4j(topic = "c.TestReentrant")
public class TestReentrant {
   static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) {
        method1();
    }

    public static void method1() {
        log.debug("执行方法1");
        try {
            lock.lock();
            log.debug("获得锁，调用方法2");
            method2();
        }finally {
            lock.unlock();
        }
    }
    public static void method2() {
        log.debug("执行方法2");
        try {
            lock.lock();
            log.debug("获得锁");
        }finally {
            lock.unlock();
        }
    }
}
